package com.wxy.ej.flow.modules.dynamicForm.controller;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.StringUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Api(tags = "JwtController -  控制类")
@RequestMapping("/coding/form/jwt")
@Controller
public class JwtController {
    @Value("${jwt.shareLink}")
    private String signature;

    // 不用放开权限，这是后台生成的
    @PreAuthorize("hasAuthority('jwt:shareLink:create')")
    @PostMapping(value = "/createShareJwt")
    @ResponseBody
    public Result createShareJwt (@RequestBody Map<String,String> dataMap) {
        String jwt = getJwt(dataMap);
        return Result.builder().setRemark(jwt).success();
    }


    //生成jwt token
    public String getJwt(Map<String,String> dataMap){
        String dynamicFormId = dataMap.get("dynamicFormId");
        String expandData = dataMap.get("expandData");
        String expDatetime = dataMap.get("expDatetime");
        Date exp = null;
        if (!StringUtils.isNull(expDatetime)) {
            exp = DateTool.string2Date(expDatetime, DateTool.DATE_YMDHMS_SPLIT_14);
        }

        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
            //header
            .setHeaderParam("typ","JWT")
            .setHeaderParam("alg","HS256")
            //payload
            .claim("dynamicFormId",dynamicFormId)
            .claim("expandData",expandData)
            .setSubject("ShareLink")
            .setExpiration(exp)
            .setId(UUID.randomUUID().toString())
            //signature
            .signWith(SignatureAlgorithm.HS256,signature)
            .compact();
        return jwtToken;
    }

    //从token中取出载荷
    public Claims getPayLoad(String jwtToken){
        Claims claims = Jwts.parser()
                .setSigningKey(signature)
                .parseClaimsJws(jwtToken)
                .getBody();
        return claims;
    }

    /*public static void main(String[] args) throws IOException {
        //生成token
        String jwtToken = new JwtController().getJwt();
        System.out.println(jwtToken);
        System.out.println(jwtToken.length());

        //从token取出载荷
        Claims claims = new JwtController().getPayLoad(jwtToken);
        System.out.println(claims);
        System.out.println(claims.get("username"));
        System.out.println(claims.get("role"));
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
    }*/


}
